<?php
class App_Ems {
	protected $_uri;
	
	protected $_path;
	
	protected $_restClient;
	
	protected $_jsonType = Zend_Json::TYPE_ARRAY;
	
	public function __construct(Zend_Uri $uri = null, $path = null) {
		if ($uri === null) {
			$this->_uri = Zend_Uri::factory ( 'http://emspost.ru/' );
		} else {
			$this->_uri = $uri;
		}
		
		if ($path === null){
		    $this->_path = '/api/rest/';
		} else {
			$this->_path = $path;
		}
		$this->_restClient = new Zend_Rest_Client ();
		$this->_restClient->setUri ( $this->_uri );
	}
	
	public function calculate($to, $weight)
	{
	    $options = array(
	       'method'=>'ems.calculate',
	       'from' => 'city--moskva' ,
	       'to'=>$to, 
	       'weight'=>$weight,
	       'plain' => true
	    );
	    return $this->restGet($options);
	}
	
	public function getTowns() {
	    $options = array ('method' => 'ems.get.locations', 'type' => 'cities', 'plain' => true );
		return $this->restGet ( $options );
	}
	
	public function restGet($options) {
		$response = $this->_restClient->restGet ( $this->_path, $options );
		return Zend_Json::decode ( $response->getBody(), $this->_jsonType );
	}
}